using System;
using System.Drawing;
using System.Runtime.CompilerServices;
namespace SS
{
	internal class EAN13Code
	{
		private const int p = 112;
		private const int P = 80;
		private int[] Pool01i;
		private char[] Pool01s;
		private int[] Pool2i;
		private int[][] Additions = new int[10][];
		private int[] ALengths;
		private Bitmap tmpCode;
		private Graphics Renderer;
		private int wdth;
		private int hght;
		public int xPos;
		public int yPos;
		private Font font;
		private string CodeVal = "";
		private string eancode = "";
		private bool valid = true;
		private int Seperators = 51;
		private float brushwidth = 1f;
		private string Name = "";
		private string Price = "";
		private string Company = "";
		private Color backcolor;
        //internal event EventHandler CodeChanged
        //{
        //    [MethodImpl(MethodImplOptions.Synchronized)]
        //    add
        //    {
        //        this.CodeChanged = (EventHandler)Delegate.Combine(this.CodeChanged, value);
        //    }
        //    [MethodImpl(MethodImplOptions.Synchronized)]
        //    remove
        //    {
        //        this.CodeChanged = (EventHandler)Delegate.Remove(this.CodeChanged, value);
        //    }
        //}
		public string m_Name
		{
			set
			{
				this.Name = value;
			}
		}
		public string m_Price
		{
			set
			{
				this.Price = value;
			}
		}
		public string m_Company
		{
			set
			{
				this.Company = value;
			}
		}
		public Bitmap TmpCode
		{
			get
			{
				return this.tmpCode;
			}
		}
		public float BrushWidth
		{
			get
			{
				return this.brushwidth;
			}
			set
			{
				this.brushwidth = value;
			}
		}
		public Color BackColor
		{
			get
			{
				return this.backcolor;
			}
			set
			{
				this.backcolor = value;
			}
		}
		public int Width
		{
			get
			{
				return this.wdth;
			}
			set
			{
				this.wdth = value;
			}
		}
		public int Height
		{
			get
			{
				return this.hght;
			}
			set
			{
				this.hght = value;
			}
		}
		internal string EANCode
		{
			get
			{
				return this.eancode;
			}
			set
			{
				this.eancode = value.Trim();
				if (this.eancode.Length == 12)
				{
					this.eancode += this.CreateChecksum(this.eancode);
				}
				if (this.eancode.Length == 13)
				{
					this.valid = true;
					if (this.CreateChecksum(this.eancode.Substring(0, 12)) != this.eancode[12].ToString())
					{
						this.valid = false;
					}
					this.CodeVal = this.CreateCode(this.eancode);
				}
				this.PrepareCode();
                //if (this.CodeChanged != null)
                //{
                //    this.CodeChanged(this, EventArgs.Empty);
                //}
			}
		}
		internal Font Font
		{
			get
			{
				return this.font;
			}
			set
			{
				this.font = value;
			}
		}
		internal void Render(Graphics Target)
		{
			if (this.tmpCode != null)
			{
				Target.DrawImage(this.tmpCode, 0, 0, this.wdth, this.hght);
			}
		}
		private void PrepareCode()
		{
			this.tmpCode = new Bitmap(this.wdth, this.hght);
			this.Renderer = Graphics.FromImage(this.tmpCode);
			this.Renderer.Clear(this.backcolor);
			this.WriteEAN(this.xPos, this.yPos, this.Renderer, this.hght * 2 / 5, this.Name, this.Price, this.Company);
		}
		private void WriteEAN(int x, int y, Graphics g, int Height, string Name, string Price, string Company)
		{
			Brush brush = (Brush)Brushes.Black.Clone();
			if (!this.valid)
			{
				brush = (Brush)Brushes.Red.Clone();
			}
			Brush brush2 = (Brush)Brushes.Transparent.Clone();
			Font font = this.font;
			if (this.eancode.Length == 13)
			{
				int num;
				if ((double)this.brushwidth >= 0.75)
				{
					num = 0;
				}
				else
				{
					if ((double)this.brushwidth >= 0.55)
					{
						num = 0;
					}
					else
					{
						num = -3;
					}
				}
				g.DrawString(Name.ToUpper(), font, Brushes.Black, (float)(x - 10), (float)(y - 15));
				g.DrawString(this.eancode.Substring(0, 1), font, Brushes.Black, (float)x - g.MeasureString(this.eancode.Substring(0, 1), font).Width - 3f, (float)(y + Height + 3 + num));
				float num2 = 0f;
				int i = 0;
				while (i < this.CodeVal.Length)
				{
					if (this.CodeVal.Substring(i, 1) == "1")
					{
						g.FillRectangle(brush, (float)x + num2, (float)y, this.brushwidth, (float)(Height + this.ALengths[i]));
					}
					else
					{
						g.FillRectangle(brush2, (float)x + num2, (float)y, this.brushwidth, (float)(Height + this.ALengths[i]));
					}
					i++;
					num2 += this.brushwidth;
				}
				num2 = this.brushwidth;
				i = 1;
				while (i <= 6)
				{
					g.DrawString(this.eancode.Substring(i, 1), font, Brushes.Black, (float)x + 14f * (num2 - 1f) + 8f, (float)(y + Height + 3 + num));
					i++;
					num2 += this.brushwidth;
				}
				num2 = this.brushwidth * 7f;
				i = 7;
				while (i < 13)
				{
					g.DrawString(this.eancode.Substring(i, 1), font, Brushes.Black, (float)x + 14f * (num2 - 7f) + 99f, (float)(y + Height + 3 + num));
					i++;
					num2 += this.brushwidth;
				}
				StringFormat stringFormat = new StringFormat();
				stringFormat.Alignment = StringAlignment.Far;
			}
		}
		internal EAN13Code(int X, int Y, int width, int height, Font fnt, Color BC)
		{
			this.backcolor = BC;
			this.tmpCode = new Bitmap(width, height);
			this.Renderer = Graphics.FromImage(this.tmpCode);
			this.wdth = width;
			this.hght = height;
			this.xPos = X;
			this.yPos = Y;
			this.font = fnt;
			this.Pool01i = new int[]
			{
				243,
				963,
				783,
				4083,
				3087,
				3843,
				3327,
				4047,
				3903,
				207,
				3135,
				3855,
				975,
				3075,
				1011,
				4035,
				51,
				771,
				195,
				831
			};
			this.Pool01s = new char[]
			{
				'p',
				'q',
				'r',
				's',
				't',
				'u',
				'v',
				'w',
				'x',
				'y',
				'P',
				'Q',
				'R',
				'S',
				'T',
				'U',
				'V',
				'W',
				'X',
				'Y'
			};
			this.Pool2i = new int[]
			{
				4035,
				3855,
				3900,
				3075,
				3324,
				3135,
				3264,
				3084,
				3120,
				4044
			};
			this.Additions[0] = new int[]
			{
				112,
				112,
				112,
				112,
				112,
				112
			};
			this.Additions[1] = new int[]
			{
				112,
				112,
				80,
				112,
				80,
				80
			};
			this.Additions[2] = new int[]
			{
				112,
				112,
				80,
				80,
				112,
				80
			};
			this.Additions[3] = new int[]
			{
				112,
				112,
				80,
				80,
				80,
				112
			};
			this.Additions[4] = new int[]
			{
				112,
				80,
				112,
				112,
				80,
				80
			};
			this.Additions[5] = new int[]
			{
				112,
				80,
				80,
				112,
				112,
				80
			};
			this.Additions[6] = new int[]
			{
				112,
				80,
				80,
				80,
				112,
				112
			};
			this.Additions[7] = new int[]
			{
				112,
				80,
				112,
				80,
				112,
				80
			};
			this.Additions[8] = new int[]
			{
				112,
				80,
				112,
				80,
				80,
				112
			};
			this.Additions[9] = new int[]
			{
				112,
				80,
				80,
				112,
				80,
				112
			};
			this.ALengths = new int[190];
			for (int i = 0; i < this.ALengths.Length; i++)
			{
				this.ALengths[i] = 0;
				if (i == 0 | i == 1 | i == 4 | i == 5 | i == 92 | i == 93 | i == 96 | i == 97 | i == 189 | i == 188 | i == 185 | i == 184)
				{
					this.ALengths[i] = 10;
				}
			}
		}
		private string CreateChecksum(string EAN)
		{
			int num = 0;
			for (int i = EAN.Length - 1; i >= 0; i--)
			{
				if (i % 2 == 0)
				{
					num += int.Parse(EAN.Substring(i, 1));
				}
				else
				{
					num += 3 * int.Parse(EAN.Substring(i, 1));
				}
			}
			int num2 = 10 - num % 10;
			return (num2 % 10).ToString();
		}
		private string CreateCode(string ean)
		{
			string str = this.Dec2Bin(this.Seperators);
			int num = int.Parse(ean.Substring(0, 1));
			for (int i = 1; i <= ean.Length; i++)
			{
				if (i <= 6)
				{
					int num2 = int.Parse(ean.Substring(i, 1));
					str = str + "00" + this.Dec2Bin(this.Pool01i[this.IndexOf(num2 + this.Additions[num][i - 1], this.Pool01s)]).PadLeft(12, '0');
				}
				else
				{
					if (i == 7)
					{
						str = str + "00" + this.Dec2Bin(this.Seperators);
					}
					else
					{
						int num2 = int.Parse(ean.Substring(i - 1, 1));
						str = str + "00" + this.Dec2Bin(this.Pool2i[num2]).PadLeft(12, '0');
					}
				}
			}
			return str + "00" + this.Dec2Bin(this.Seperators);
		}
		private int IndexOf(int CharID, char[] Array)
		{
			int result = -1;
			char c = (char)CharID;
			for (int i = 0; i < Array.Length; i++)
			{
				if (Array[i] == c)
				{
					result = i;
					break;
				}
			}
			return result;
		}
		private string Dec2Bin(int Number)
		{
			int num = Number;
			double num2 = Math.Log((double)Number, 2.0);
			num2 -= 0.5;
			string text = "";
			int num3 = (int)Math.Round(num2, 0);
			for (int i = num3; i >= 0; i--)
			{
				int num4 = (int)Math.Pow(2.0, (double)i);
				if (num4 <= num)
				{
					num -= num4;
					text += "1";
				}
				else
				{
					text += "0";
				}
			}
			return text;
		}
	}
}
